import os
import time

from Baidu_Text_transAPI import translateBaidu
def printPath(level,path):
    # 所有文件
    fileList = []
    # 返回一个列表，其中包含在目录条目的名称
    files = os.listdir(path)
    for f in files:
        if(os.path.isfile(path + '/' + f)):
            # 添加文件
            if f.split('.')[1]=='ann':
                fileList.append(f)
    # 当一个标志使用，文件夹列表第一个级别不打印
    f2 = open("anndata.txt", 'w', encoding='utf-8')
    enstr=''
    for fl in fileList:
        # 打印文件
        print(fl)
        f = open(path+ '/' + fl,encoding='utf-8')#读取完txt再读txt里面的类容
        # print(f.read())
        alllines = f.readlines()
        for eachLine in alllines:
            if eachLine[0]=='T':
                for word in eachLine.split()[4:]:
                    enstr+= word +' '
                enstr+='\n'
        resultlist = translateBaidu(enstr)
        enstr = ''
        for d in resultlist:
            d['src']

        f2.write(chstr + '\n')
        f2.write(enstr + '\n')
        time.sleep(1)
    f2.close()
    print('文件数',len(fileList))
if __name__ == '__main__':
    printPath(1, 'TFH_Annotated_Dataset')
    # print(translateBaidu('An insulating film substantially free from surface irregularities is RF bias sputtered onto a smooth polycrystalline or micro-roughened surface. Controlled sputtering is performed first at a low reemission coefficient and then, after a substantially continuous layer of insulative amorphous film is deposited over the substrate, increasing the reemission coefficient to a second higher level. A low reemission coefficient is about 0.25 and a high coefficient is about 0.7.'))